<?php

namespace app\api\controller\product;

use app\api\model\product\Product as ProductModel;
use app\api\controller\Controller;
use app\common\model\product\Recommend as RecommendModel;
use app\common\model\product\RecommendList;
use think\facade\Cache;

/**
 * 商品推荐控制器
 */
class Recommend extends Controller
{
    /**
     * 商品推荐分类
     */
    public function lists()
    {
        // 整理请求的参数
        $param['status'] = 1;
        $user = $this->getUser();
        $Shopinfo = $this->getUserCache();
        $param['shop_supplier_id'] = $Shopinfo['shop'];
        $param['audit_status'] = 1;

            // 获取列表数据
        $list = RecommendModel::getALL($param);
        return $this->renderSuccess('', compact('list'));
    }

    /**
     * 按分类输出商品列表
     */
    public function categorylists()
    {
        $requestData = $this->postData();

        if (!isset($requestData['module_id'])) {
            return $this->renderError('缺少module_id参数');
        }

        $productIds = (new RecommendList)
            ->where(['module_id' => $requestData['module_id'], 'status' => 1])
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->column('product_id');
        
        if (!$productIds) {
            return $this->renderSuccess();
        }

        // 整理请求的参数
        $param = array_merge($requestData, [
            'product_status' => 10,
            'audit_status'   => 10,
            'is_show'        => 1,    //显示可搜索
            'product_ids'    => $productIds,
            'module_id'      => $requestData['module_id'],
        ]);
        // 获取列表数据
        $model = new ProductModel;
        $list = $model->getList($param, $this->getUser(false), 3);
        return $this->renderSuccess('', compact('list'));
    }
}
